package com.chy.blog_server.modular.web.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.modular.web.entity.BoContent;
import com.chy.blog_server.modular.web.entity.BoImgLog;
import com.chy.blog_server.modular.web.mapper.BoContentMapper;
import com.chy.blog_server.modular.web.service.IBoContentService;
import com.chy.blog_server.modular.web.service.IBoImgLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：内容表 service业务层
 * 作者：chenhuiyan
 * 时间：2020/02/23 12:03
 */
@Service
@Transactional(readOnly = true)
public class BoContentServiceImpl extends ServiceImpl<BoContentMapper, BoContent> implements IBoContentService {

    @Autowired
    private IBoImgLogService boImgLogService;

    private final static Logger logger = LoggerFactory.getLogger(BoContentServiceImpl.class);

    /**
     * 查询列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> list(Map<String, Object> params){
        return baseMapper.list(params);
    }

    /**
     * 查询总数
     * @param params
     * @return
     */
    public int getCount(Map<String, Object> params){
        return baseMapper.getCount(params);
    }

    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    public Map<String, Object> getInfoById(String id){
        return baseMapper.getInfoById(id);
    }

    /**
     * 保存
     * @param boContent
     * @param imgName 保存编辑器上传的图片名，用于记录相应的内容图片
     * @param imgSize 对应的图片大小
     * @param request
     * @return
     */
    @Transactional(readOnly = false)
    public Map<String, Object> saveData(BoContent boContent, String imgName, String imgSize, HttpServletRequest request){
        Map<String, Object> res = new HashMap<>();
        if(StrUtil.isEmpty(boContent.getId())){
            boContent.preInsert(request);
            //保存图片记录
            if(!StrUtil.isEmpty(imgName)){
                String[] imgNameArr = imgName.split(","); //图片名
                Integer[] imgSizeArr = Convert.toIntArray(imgSize.split(",")); //图片大小
                List<BoImgLog> boImgLogList = new ArrayList<>();
                for(int index = 0; index < imgNameArr.length; index++){
                    BoImgLog boImgLog = new BoImgLog();
                    boImgLog.preInsert();
                    boImgLog.setImgName(imgNameArr[index]);
                    boImgLog.setImgSize(imgSizeArr[index]);
                    boImgLog.setTableName("bo_content");
                    boImgLog.setTableId(boContent.getId());
                    boImgLog.setImgPath(UploadURLUtil.CONTENT_IMAGE_PATH);
                    boImgLogList.add(boImgLog);
                }
                if(boImgLogList.size() > 0){
                    boImgLogService.saveBatch(boImgLogList);
                }
            }
            this.save(boContent);
            res.put("state", "200");
            res.put("message", "添加成功");
        }else{
            //保存图片记录
            if(!StrUtil.isEmpty(imgName)){
                String[] imgNameArr = imgName.split(","); //图片名
                Integer[] imgSizeArr = Convert.toIntArray(imgSize.split(",")); //图片大小
                List<BoImgLog> boImgLogList = new ArrayList<>();
                for(int index = 0; index < imgNameArr.length; index++){
                    BoImgLog boImgLog = new BoImgLog();
                    boImgLog.preInsert();
                    boImgLog.setImgName(imgNameArr[index]);
                    boImgLog.setImgSize(imgSizeArr[index]);
                    boImgLog.setTableName("bo_content");
                    boImgLog.setTableId(boContent.getId());
                    boImgLog.setImgPath(UploadURLUtil.CONTENT_IMAGE_PATH);
                    boImgLogList.add(boImgLog);
                }
                if(boImgLogList.size() > 0){
                    boImgLogService.saveBatch(boImgLogList);
                }
            }
            boContent.preUpdate(request);
            this.updateById(boContent);
            res.put("state", "200");
            res.put("message", "修改成功");
        }
        return res;
    }

    /**
     * 查询已删除的内容列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> recycleList(Map<String, Object> params){
        return baseMapper.recycleList(params);
    }

    /**
     * 彻底删除
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int thoroughlyDelete(String id){
        //查询该编辑对应的图片记录
        List<BoImgLog> imgLogList = boImgLogService.getBaseMapper()
                .selectList(new QueryWrapper<BoImgLog>()
                        .eq("table_name", "bo_content")
                        .eq("table_id", id));

        //删除图片
        boolean flag = true;
        if(imgLogList.size() > 0){
            String path = UploadURLUtil.CONTENT_IMAGE; //路径
            String[] imgName = new String[imgLogList.size()];
            for (int i = 0; i < imgLogList.size(); i++){
                imgName[i] = path + imgLogList.get(i).getImgName();
            }
            flag = UploadFileUtil.deleteMoreFile(imgName);
            logger.info(">>>>>>>>>>>>删除图片成功<<<<<<<<<<<<<<<<");
        }

        //删除图片成功则删表记录
        if(flag){
            boImgLogService.getBaseMapper().delete(new QueryWrapper<BoImgLog>().eq("table_name", "bo_content")
                    .eq("table_id", id));
            logger.info(">>>>>>>>>>>>删除图片表记录成功<<<<<<<<<<<<<<<<");

            //删除内容记录
            int i = baseMapper.thoroughlyDelete(id);
            logger.info(">>>>>>>>>>>>删除内容记录成功<<<<<<<<<<<<<<<<");
            return i;

        }else{
            return 0;
        }

    }

}
